package com.spider.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.spider.constant.YjsUrl;
import com.spider.entity.YjsPriceEntity;
import com.spider.mapper.HeownsMapper;
import com.spider.util.StringUtil;
import com.spider.util.YjsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 此类为测试用
 */
@RestController
public class TestController {
    private final Logger logger = LoggerFactory.getLogger(TestController.class);
    @Autowired
    HeownsMapper mapper;
    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("/")
    public String hello(){
        //拼接参数
        String url= YjsUrl.PRODUCT_URL+YjsUrl.PRICE_UPDATE;
        JSONObject apiinfo= YjsUtil.getApiinfo();
        try{
            JSONArray body= YjsUtil.getBody();

            JSONObject jb=new JSONObject(new LinkedHashMap());
            jb.put("apiinfo",apiinfo);
            jb.put("price",body);
            System.out.println(jb);
            ResponseEntity<String> result=restTemplate.postForEntity(url,jb,String.class);
            String res=StringUtil.unicodeDecode(result.toString());
            System.out.println(res);
        }catch (Exception e){
            logger.error("有机所价格推送失败，失败原因："+e.getMessage());
        }
        return null;
    }

    @RequestMapping("/hello")
    public String hello2(){
        logger.info("有机所价格推送开始：日期"+new Date().toString());
        //拼接参数
        String url= YjsUrl.PRODUCT_URL+YjsUrl.PRICE_UPDATE;
        JSONObject apiinfo= YjsUtil.getApiinfo();
        //拼接body
        List<YjsPriceEntity> list = mapper.selectByDate();
        if(list.isEmpty()){
            logger.info("有机所价格推送无数据，推送结束");
        }else{
            logger.info("有机所价格推送查询结果集数量为"+list.size());
            for(YjsPriceEntity entity:list){
                try{
                    JSONArray body= YjsUtil.getBody(entity);
                    if(body==null){
                        logger.info("实体拼接数据失败缺乏数据，跳过此数据:"+entity.toString());
                    }
                    JSONObject jb=new JSONObject(new LinkedHashMap());
                    jb.put("apiinfo",apiinfo);
                    jb.put("price",body);
                    System.out.println(jb);
                    ResponseEntity<String> result=restTemplate.postForEntity(url,jb,String.class);
                    String res= StringUtil.unicodeDecode(result.toString());
                    System.out.println(res);
                }catch (Exception e){
                    logger.error("有机所价格推送失败，推送实体是："+entity.toString());
                    logger.error("有机所价格推送失败，失败原因："+e.getMessage());
                }

            }
        }
        logger.info("有机所价格推送结束：日期"+new Date().toString());
        return "hello world3";
    }
}
